/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rfid;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xandru
 */
public class GestorTags extends HashMap<String, TagData> implements Runnable {

    Thread hilo;
    boolean parar=false;
    long interval=500;
    ArrayList<RFIDEventListener> listeners = new ArrayList<RFIDEventListener>();

    public void setInterval(long i)
    {
       interval = i;
    }

    void update(String key, TagData t) {
        //System.out.println("Actualizando " + t);
        TagData tmp = this.get(key);
        //System.out.println(tmp.getLastSeenTimestampUTC() + " " + t.getLastSeenTimestampUTC() + " Antena:" + t.getAntennaID());
        tmp.setLastSeenTimestampUTC(new Date().getTime());
        for (RFIDEventListener listener : listeners) {
            listener.onUpdateTag(new RFIDTagEvent(this,"Actualizacion tag",RFIDTagEvent.UPDATE_TAG, t));
            //System.out.println(tmp.getAntennaID() + " vs " + t.getAntennaID());
        }
    }

    void save(String fichero) throws FileNotFoundException, IOException
    {
        boolean append = false;

      PrintWriter out = new PrintWriter(new FileWriter(fichero + ".txt",append));
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss,S");

      for (TagData tag : this.values()) {
            out.println(tag.getEPCParameter() + "|" + sdf.format(new Date(tag.getFirstSeenTimestampUTC())) + "|" + sdf.format(new Date(tag.getLastSeenTimestampUTC())));
        }
      out.close();
    }


    void print() {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss,S");
        for (TagData tag : this.values()) {
            //System.out.println(tag.getEPCParameter() + " " + tag.getFirstSeenTimestampUTC() + " " + tag.getLastSeenTimestampUTC() + " " + new Date().getTime());
            //System.out.println(sdf.format(new Date(tag.getFirstSeenTimestampUTC())) + " " + sdf.format(new Date(tag.getLastSeenTimestampUTC())));
        }


    }

    void add(String key, TagData tagData) {
        this.put(key, tagData);
        for (RFIDEventListener listener : listeners) {
            System.out.println("Nuevo Tag Leido. Antena " + tagData.getAntennaID());
            listener.onNewTag(new RFIDTagEvent(this,"Nuevo Tag Leido",RFIDTagEvent.NEW_TAG, tagData));
        }
    }

    void addListener(RFIDEventListener l)
    {
       listeners.add(l);
    }

    public void run() {
        while (!parar)
        {
            try {
                hilo.sleep(interval);
            } catch (InterruptedException ex) {
                Logger.getLogger(GestorTags.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        
    }

    void start() {
        if (hilo == null) {
            hilo = new Thread(this);
            hilo.start();
        }
    }
}
